<template>
  <div class="login-container">
    <h2>能耗管理系统登录</h2>
    <form @submit.prevent="handleLogin">
      <div class="form-group">
        <label>用户名:</label>
        <input
          type="text"
          v-model="form.username"
          required
          placeholder="请输入用户名"
        />
      </div>
      <div class="form-group">
        <label>密码:</label>
        <input
          type="password"
          v-model="form.password"
          required
          placeholder="请输入密码"
        />
      </div>
      <button type="submit" :disabled="loading">
        {{ loading ? "登录中..." : "登录" }}
      </button>
      <p class="register-link">
        没有账号？<router-link to="/register">立即注册</router-link>
      </p>
    </form>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { mockLogin } from "../api/mockAuth";

const router = useRouter();
const form = ref({
  username: "",
  password: "",
});
const loading = ref(false);

const handleLogin = async () => {
  try {
    loading.value = true;
    const success = await mockLogin(form.value);
    if (success) {
      router.push("/dashboard");
    }
  } finally {
    loading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  max-width: 400px;
  margin: 2rem auto;
  padding: 2rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.form-group {
  margin-bottom: 1.5rem;
}

input {
  width: 100%;
  padding: 0.5rem;
  margin-top: 0.5rem;
}

button {
  width: 100%;
  padding: 0.75rem;
  background-color: #2c3e50;
  color: white;
  border: none;
  cursor: pointer;
}

.register-link {
  margin-top: 1rem;
  text-align: center;
}
</style>
